//
//  ZJDealVC.m
//  这家测试项目
//
//  Created by IOS_DEVELOPER on 2017/7/10.
//  Copyright © 2017年 陈志凌. All rights reserved.
//

#import "ZJDealVC.h"
#import "ZJDealModel.h"
#import "ZJDealCell.h"

@interface ZJDealVC ()<UITableViewDelegate,UITableViewDataSource>{
    NSInteger page;
}
@property (weak, nonatomic) IBOutlet UITableView *tableview;
@property (strong, nonatomic) NSMutableArray * dataArr;
@end

@implementation ZJDealVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableview registerNib:[UINib nibWithNibName:NSStringFromClass([ZJDealCell class]) bundle:nil] forCellReuseIdentifier:@"ZJDealCell"];
    page = 1;
    [self sendRequest];
    [self setupRefresh];
    
    [self.tableview setSeparatorColor:[Utils colorWithHexString:@"dddddd"]];
}

- (void)setupRefresh
{
    WEAK_SELF
    [self.tableview setRefreshWithHeaderBlock:nil footerBlock:^{
        STRONG_SELF
        page++;
        [strongSelf sendRequest];
    }];
    
}

-(void)sendRequest{
    [HUD showLoading:@"加载数据中..."];
    NSDictionary *parameter = [LRNetwork requestParameter:@{@"page":@(page)}];
    NSString *url = [LRNetwork requestURL:@"cgi/trade.php"];
    
    [LRNetwork netRequestPOSTWithRequestURL:url withParameter:parameter withReturnValeuBlock:^(id returnValue) {

        NSArray *arr = [ZJDealModel mj_objectArrayWithKeyValuesArray:returnValue[@"trade"]];
        if (page == 1) {
            self.dataArr = arr.mutableCopy;
        } else {
            [self.dataArr addObjectsFromArray:arr];
        }
        if (!self.dataArr) {
            self.dataArr = arr.mutableCopy;
        }
        [self.tableview reloadData];
        [HUD hide];

    } withErrorCodeBlock:^(NSInteger errorCode, NSString *errorMessage) {
        [HUD showErrorMsg:errorMessage];
    } withFailureBlock:^{
        
    }];
    
    [self.tableview headerEndRefreshing];
    [self.tableview footerEndRefreshing];
}

#pragma mark - Table view data source



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    [tableView tableViewDisplayWitMsg:kNoDataImgName ifNecessaryForRowCount:self.dataArr.count];
    return self.dataArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZJDealCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZJDealCell"];
    ZJDealModel * model = self.dataArr[indexPath.row];
    cell.model = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    [cell useCellFrameCacheWithIndexPath:indexPath tableView:tableView];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - 补全分隔线左侧缺失
- (void)viewDidLayoutSubviews {
    if ([self.tableview respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableview setSeparatorInset:UIEdgeInsetsZero];
        
    }
    if ([self.tableview respondsToSelector:@selector(setLayoutMargins:)])  {
        [self.tableview setLayoutMargins:UIEdgeInsetsZero];
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}
@end
